package com.xafc.demo.sys.controller.api;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xafc.demo.sys.model.dto.RoleDto;
import com.xafc.demo.sys.model.entity.Menu;
import com.xafc.demo.sys.model.entity.Role;
import com.xafc.demo.framework.result.ApiResult;
import com.xafc.demo.framework.result.PageResult;
import com.xafc.demo.framework.result.ResultCode;
import com.xafc.demo.sys.model.query.RolePageQuery;
import com.xafc.demo.sys.service.IRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Api(tags="角色相关接口")
@RestController
@RequestMapping("/api/sys/role")
public class RoleApi {

    @Autowired
    private IRoleService roleService;

    @ApiOperation(value = "根据分页获取角色列表")
    @PreAuthorize("hasAuthority('API_SYS_ROLE_LIST')")
    @GetMapping("/list")
    public PageResult<RoleDto> list(RolePageQuery query){
        Page<RoleDto> page = this.roleService.getRoleList(query);
        return PageResult.success(page);
    }

    @ApiOperation(value = "根据ID获取角色信息")
    @PreAuthorize("hasAuthority('API_SYS_ROLE_UPDATE')")
    @GetMapping("/get/{id}")
    public ApiResult<Role> detail(@PathVariable("id") Integer id){
        Role role = roleService.getRoleById(id);
        return ApiResult.success(role);
    }

    @ApiOperation(value = "根据IDs获取多个角色信息")
    @GetMapping("/get")
    public ApiResult<List<Role>> get(@RequestParam("ids") String ids){
        List<Integer> idLists = Arrays.stream(ids.split(",")).map(o -> Integer.valueOf(String.valueOf(o))).collect(Collectors.toList());
        List<Role> roleList = roleService.getRoleByIds(idLists);
        return ApiResult.success(roleList);
    }

    @ApiOperation(value = "新增角色")
    @PreAuthorize("hasAuthority('API_SYS_ROLE_ADD')")
    @PostMapping("/add")
    public ApiResult add(Role role){
        ResultCode resultCode = roleService.addRole(role);
        return ApiResult.value(resultCode);
    }

    @ApiOperation(value = "更新角色")
    @PreAuthorize("hasAuthority('API_SYS_ROLE_UPDATE')")
    @PostMapping("/update")
    public ApiResult edit(Role role){
        ResultCode resultCode = roleService.updateRole(role);
        return ApiResult.value(resultCode);
    }

    @ApiOperation(value = "删除角色")
    @PreAuthorize("hasAuthority('API_SYS_ROLE_DELETE')")
    @PostMapping("/delete")
    public ApiResult delete(@RequestParam("id") Integer id){
        ResultCode resultCode = roleService.deleteRole(id);
        return ApiResult.value(resultCode);
    }

    @ApiOperation(value = "废弃角色")
    @PreAuthorize("hasAuthority('API_SYS_ROLE_DISCARD')")
    @PostMapping("/discard")
    public ApiResult discard(@RequestParam("id") Integer id){
        ResultCode resultCode = roleService.discardRole(id);
        return ApiResult.value(resultCode);
    }

    @ApiOperation(value = "角色授权")
    @PreAuthorize("hasAuthority('API_SYS_ROLE_GRANT_PERMISSION')")
    @PostMapping("/grant/permission")
    public ApiResult grantPermission(@RequestParam("roleId") Integer roleId,
                                     @RequestParam("menuIds") String menuIds){
        List<Integer> menuIdList = new ArrayList<>();
        if(StrUtil.isNotEmpty(menuIds)){
            menuIdList = Arrays.stream(menuIds.split(",")).map(Integer::parseInt).collect(Collectors.toList());
        }
        ResultCode resultCode = roleService.grantPermission(roleId, menuIdList);
        return ApiResult.value(resultCode);
    }

    @ApiOperation(value = "根据ID角色授权信息")
    @PreAuthorize("hasAuthority('API_SYS_ROLE_GRANT_PERMISSION')")
    @GetMapping("/get/permission")
    public ApiResult getPermission(@RequestParam("roleId") Integer roleId){
        List<Menu> menuList = roleService.getPermission(roleId);
        List<String> menuIdList = menuList.stream().map(Menu::getId).map(Object::toString).collect(Collectors.toList());
        String menuIds = String.join(",", menuIdList);
        return ApiResult.success(menuIds);
    }
}
